export default {
  methods: {
    // 获取影片用券后价格
    getFilmDiscount() {
      if (this.preInfo.film) {
        const filmDiscount = this.preInfo.film.discount
        return this.$utils.isNaNStr(filmDiscount)
      }
      return 0
    },

    // 获取卖品用券后价格
    getGoodsDiscount() {
      if (this.preInfo.goods) {
        let goodsDiscount = this.preInfo.goods.discount
        goodsDiscount = this.$utils.isNaNStr(goodsDiscount)
        return goodsDiscount
      }
      return 0
    },

    // 计算最大可用积分总积分
    caclMaxIntegral() {
      const maxIntegral = this.$utils.isNaNStr(this.accountInfo.maxIntegral) // 本日可用积分上限
      let nowMaxIntegral =
        this.caclMaxFilmIntegral() + this.caclMaxGoodsIntegral() // 卖品积分 + 商品可用积分
      const ableIntegral = this.balance2Intergral(
        this.getGoodsDiscount() + this.getFilmDiscount()
      ) // 需要花费的金额对于积分
      nowMaxIntegral =
        nowMaxIntegral > ableIntegral ? ableIntegral : nowMaxIntegral

      return maxIntegral > nowMaxIntegral ? nowMaxIntegral : maxIntegral
    },

    // 计算最大影片可用积分
    caclMaxFilmIntegral() {
      if (!this.accountInfo.integralFilm) {
        return 0
      }
      const filmDiscount = this.getFilmDiscount() // 需要付的影票钱
      const filmTotal = this.caclFilmTotal() // 影票钱总价格

      const integralFilm = this.accountInfo.integralFilm
      const eqlRatio = this.$utils.isNaNStr(this.accountInfo.eqlRatio)
      const payRatio = this.$utils.isNaNStr(integralFilm.payRatio)
      const maxIntegral = this.$utils.isNaNStr(integralFilm.maxIntegral)
      const integral = filmTotal * eqlRatio * payRatio // 整个影票可用的积分
      const needPayIntegral = filmDiscount * eqlRatio // 需要付的影票钱对应的积分
      const result = this.$utils.getMin([
        integral,
        maxIntegral,
        needPayIntegral
      ])
      return this.$utils.floor(result) // 向下取整
    },

    // 计算最大卖品可用积分
    caclMaxGoodsIntegral() {
      if (!this.accountInfo.integralGoods) {
        return 0
      }
      const goodsDiscount = this.getGoodsDiscount() // 需要付的卖品/商品钱
      const goodsTotal = this.caclGoodsTotal() // 卖品/商品总价

      const integralGoods = this.accountInfo.integralGoods
      const eqlRatio = this.$utils.isNaNStr(this.accountInfo.eqlRatio)
      const payRatio = this.$utils.isNaNStr(integralGoods.payRatio)
      const maxIntegral = this.$utils.isNaNStr(integralGoods.maxIntegral)
      const integral = goodsTotal * eqlRatio * payRatio // 整个 卖品/商品 可用的积分
      const needPayIntegral = goodsDiscount * eqlRatio // 需要付的影票钱对应的积分
      const result = this.$utils.getMin([
        integral,
        maxIntegral,
        needPayIntegral
      ])
      return this.$utils.floor(result) // 向下取整
    },

    // 计算最大可花积分
    calcMaxIntegralCut(flag = false) {
      if (!flag && !this.isCheck(this.INTEGRAL)) {
        return 0
      }
      const myIntegral = this.$utils.isNaNStr(this.accountInfo.integral) // 我的积分
      const allIntegral = this.caclMaxIntegral() // 最大可用积分
      const ableIntegral = myIntegral > allIntegral ? allIntegral : myIntegral // 可花费积分
      return ableIntegral
    },

    // 换算积分=》金额
    integral2balance(integral) {
      integral = this.$utils.isNaNStr(integral)
      const eqlRatio = this.$utils.isNaNStr(this.accountInfo.eqlRatio)
      const money = integral / eqlRatio

      return money
    },

    balance2Intergral(balance) {
      balance = this.$utils.isNaNStr(balance)
      const eqlRatio = this.$utils.isNaNStr(this.accountInfo.eqlRatio)
      const money = balance * eqlRatio

      return money
    },

    // 计算积分抵扣金额
    caclIntegralCut() {
      const ableIntegral = this.calcMaxIntegralCut() // 可花费积分
      return this.integral2balance(ableIntegral)
    },

    // 计算余额抵扣（共花费余额）
    caclBalanceCut() {
      if (this.isCheck(this.BALANCE)) {
        // 勾选余额
        const needPay = this.needPay
        const maxBalance = needPay - this.caclIntegralCut() // 最大可用余额
        const myBalance = this.$utils.isNaNStr(this.accountInfo.balance) // 我的余额
        const ableBalance = maxBalance > myBalance ? myBalance : maxBalance // 可用余额
        return ableBalance
      }
      return 0
    },

    // 计算影片花费积分
    caclFilmIntegralCut() {
      const calcMaxIntegralCut = this.calcMaxIntegralCut() // 已花费积分
      const caclMaxFilmIntegral = this.caclMaxFilmIntegral() // 最大影票积分
      return calcMaxIntegralCut > caclMaxFilmIntegral
        ? caclMaxFilmIntegral
        : calcMaxIntegralCut
    },

    // 计算影片花费积分抵用的金额
    caclFilmIntegral2Money() {
      const caclFilmIntegralCut = this.caclFilmIntegralCut()
      return this.integral2balance(caclFilmIntegralCut)
    },

    // 计算卖品花费积分
    caclGoodsIntegralCut() {
      const calcMaxIntegralCut =
        this.calcMaxIntegralCut() - this.caclFilmIntegralCut() // 已花费积分 (影片优先级大于卖品)
      const caclMaxGoodsIntegral = this.caclMaxGoodsIntegral() // 最大可用卖品积分
      return calcMaxIntegralCut > caclMaxGoodsIntegral
        ? caclMaxGoodsIntegral
        : calcMaxIntegralCut
    },

    // 计算卖品花费积分抵用的金额
    caclGoodsIntegral2Money() {
      const caclGoodsIntegralCut = this.caclGoodsIntegralCut()
      return this.integral2balance(caclGoodsIntegralCut)
    },

    // 计算影片花费多少余额
    caclFilmBalanceCut() {
      const caclBalanceCut = this.caclBalanceCut() // 花费的余额
      const caclFilmIntegral2Money = this.caclFilmIntegral2Money() // 花费积分抵用的金额
      const getFilmDiscount = this.getFilmDiscount() // 共需要支付的影片金额
      const maxFilmBalance = getFilmDiscount - caclFilmIntegral2Money // 最大影片可用余额

      return maxFilmBalance > caclBalanceCut ? caclBalanceCut : maxFilmBalance
    },

    // 计算卖品花费多少余额
    caclGoodsBalanceCut() {
      const caclBalanceCut = this.caclBalanceCut() - this.caclFilmBalanceCut() // 花费的余额  (影片优先级大于卖品)
      const caclGoodsIntegral2Money = this.caclGoodsIntegral2Money()
      const getGoodsDiscount = this.getGoodsDiscount() // 共需要支付的影片金额
      const maxGoodsBalance = getGoodsDiscount - caclGoodsIntegral2Money
      return maxGoodsBalance > caclBalanceCut ? caclBalanceCut : maxGoodsBalance
    },

    // 计算影片原价
    caclFilmTotal() {
      if (this.preInfo.film) {
        const filmPrice = this.preInfo.film.filmPrice
        return this.$utils.isNaNStr(filmPrice)
      }
      return 0
    },

    // 计算商品原价
    caclGoodsTotal() {
      if (this.preInfo.goods) {
        const price = this.preInfo.goods.price
        return this.$utils.isNaNStr(price)
      }
      return 0
    }
  }
}
